import fly from '@/assets/js/fly.js'

function check_login (islogin) {
    let openid
    wx.checkSession({
        success: islogin,
        fail: function () {
            // session_key 已经失效，需要重新执行登录流程
            // 调微信授权登录
            wx.login({
                success: function (result) {
                    if (result.code) {
                        //发起网络请求
                        fly
                            .post(
                                "https://api.weixin.qq.com/sns/jscode2session?appid=wx068421b4fbf58332&secret=4da0ff999942b5cc3b8ee7ae05462ba7&js_code=" +
                                result.code +
                                "&grant_type=authorization_code"
                            )
                            .then(response => {
                                let res = response.data;
                                openid = res.openid
                                wx.setStorage({
                                    key: 'openid',
                                    data: res.data
                                });
                                fly
                                    .post("api/v4_2/login", {
                                        mode: 4,
                                        openid: res.openid
                                    })
                                    .then(re => {
                                        let result = re.data;
                                        if (result.status_code == 1017) {
                                            // 未绑定手机号
                                            wx.navigateTo({
                                                url: "/pages/package_mine/bind_tel/main?openid=" + openid
                                            });
                                        } else if (result.status_code == 0) {
                                            wx.setStorage({
                                                key: "user_id",
                                                data: result.data.user_id
                                            });
                                            wx.setStorage({
                                                key: "user_token",
                                                data: result.data.token,
                                                success () {
                                                    wx.showToast({
                                                        title: "登录成功",
                                                        icon: "none",
                                                        duration: 2000,
                                                        mask: true
                                                    });
                                                    // islogin()
                                                    return true
                                                }
                                            });

                                        }
                                    })
                                    .catch(err => {
                                        wx.showToast({
                                            title: "请求失败，请重试",
                                            icon: "none",
                                            duration: 2000,
                                            mask: true
                                        });
                                        return false
                                    });
                            })
                            .catch(err => {
                                wx.showToast({
                                    title: "登录失败",
                                    icon: "none",
                                    duration: 2000,
                                    mask: true
                                });
                                return false
                            });
                    } else {
                        wx.showToast({
                            title: "登录失败",
                            icon: "none",
                            duration: 2000,
                            mask: true
                        });
                        return false
                    }
                },
                fail () {
                    wx.showToast({
                        title: "无法获取登录信息",
                        icon: "none",
                        duration: 2000,
                        mask: true
                    });
                    return false;
                }
            });
        }
    });
}

export default check_login
